from typing import List

from common.logger import logger
from model.error import ParkSpiderError, ParkSpiderErrorCode
from proxy.empty_proxy_service import EmptyProxyService
from proxy.tunnel_proxy_service_qg import TunnelProxyService_QG
from proxy.tunnel_proxy_service_kdl import TunnelProxyService_KDL
from proxy.tunnel_proxy_service_xx import TunnelProxyService_XX
from proxy.proxy_service import ProxyService
from model.proxy import Proxy

class ProxyManager:
    def __init__(self) -> None:
        self.__services: List[ProxyService] = []

    def add_service(self, url: str):
        if url == "kdl" or not url:
            self.__services.append(TunnelProxyService_KDL())
        elif url == "xx":
            self.__services.append(TunnelProxyService_XX())
        elif url == "empty":
            self.__services.append(EmptyProxyService())
        elif url == "qg":
            self.__services.append(TunnelProxyService_QG())

    def take_proxy(self, platformId: int) -> str:
        if not self.__services:
            return None
        return self.__services[-1].take(platformId)

    def invalid_proxy(self, platformId: int, proxy: Proxy):
        if not self.__services:
            return
        self.__services[-1].invalid(platformId, proxy)
